#!/bin/sh
set -e

cd "$(dirname $0)"/..

. ./scripts/version

REPO=${REPO:-rancher}

echo '#!/bin/sh' >push.sh
chmod +x push.sh

docker manifest inspect docker:18.09 | jq -r '.manifests[] | "\(.digest) \(.platform.architecture)"' | while read HASH ARCH; do
    BIN_ARCH=${ARCH}
    if [ "${ARCH}" = amd64 ];then
        BIN_ARCH=x86_64
    fi
    cat > Dockerfile.gen << EOF
FROM docker@${HASH}
COPY bin/dapper-Linux-${BIN_ARCH} /usr/local/bin/dapper
EOF
    docker build -f Dockerfile.gen -t ${REPO}/dapper:${VERSION}-linux-${ARCH} .
    echo "docker push ${REPO}/dapper:${VERSION}-linux-${ARCH}" >> push.sh
done

echo manifest-tool push from-args --platforms linux/arm,linux/arm64,linux/amd64 --template ${REPO}/dapper:${VERSION}-OS-ARCH --target ${REPO}/dapper:${VERSION} >> push.sh
echo '#######' push.sh
cat ./push.sh
echo '#######' push.sh
echo run ./push.sh
